package com.ysp.summary.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.google.common.base.Stopwatch;
import com.ysp.summary.entity.RespResult;
import com.ysp.summary.entity.Student;
import com.ysp.summary.service.StudentService;
import com.ysp.summary.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 利用便捷工具类 实现Excel的操作
 * hutool啊
 */
@Slf4j
@RestController
@RequestMapping("excel")
public class ExcelController {

    /**
     * 计时器
     */
    public static Stopwatch stop = Stopwatch.createUnstarted();

    @Autowired
    private StudentService studentService;

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;


    @GetMapping("downloadTemplate")
    public void getExcelTemplate(){
        stop.start();
        ExcelWriter excelWriter = new ExcelWriter();
//        ExcelWriter excelWriter2 = ExcelUtil.getBigWriter();
        Sheet sheet = excelWriter.getSheet();
        sheet.setColumnWidth(0,15*256);
        sheet.setColumnWidth(1,15*256);
        sheet.setColumnWidth(2,15*256);
        sheet.setColumnWidth(3,15*256);
        String[] title = {"学号","姓名","年龄","班级"};
        excelWriter.writeRow(Arrays.asList(title));
        List<Student> list = studentService.getStudentList();
        setStudent(list);
        int row=0;
        for (int i = 0; i < list.size(); i++) {
            row++;
            int col =0;
            excelWriter.writeCellValue(col++,row,list.get(0).getId());
            excelWriter.writeCellValue(col++,row,list.get(1).getName());
            excelWriter.writeCellValue(col++,row,list.get(2).getAge());
            excelWriter.writeCellValue(col++,row,list.get(3).getClassName());
        }
        ExcelUtil.writerExcelFile("学生信息",response,excelWriter);
        stop.stop();
        log.info("导出Excel文件 耗时："+stop.elapsed(TimeUnit.MILLISECONDS));
        stop.reset();
    }

    private void setStudent(List<Student> list) {
        Student stu =null;
        for (int i = 0; i < 10; i++) {
            stu =new Student();
            stu.setId(Long.valueOf(RandomUtil.randomNumbers(5)));
            stu.setAge(Integer.valueOf(RandomUtil.randomNumbers(3)));
            stu.setName(RandomUtil.randomString("赵钱孙李一帆风顺",2));
            stu.setClassName(Integer.valueOf(RandomUtil.randomNumbers(2))+"班");
            list.add(stu);
        }
    }

    @GetMapping("getList")
    public RespResult<List<Student>> getStudeneList(){
        String req = request.getRequestURI();
        return RespResult.success(studentService.selectStudent());
    }

    @GetMapping("get/{id}")
    public RespResult<Student> getStudeneList(@PathVariable long id){
        String req = request.getRequestURI();
        return RespResult.success(studentService.getStudent(id));
    }
}
